package com.yunhe.action;

import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionSupport;
import com.yunhe.biz.IItemBiz;
import com.yunhe.biz.IOptionBiz;
import com.yunhe.biz.ISubjectBiz;
import com.yunhe.biz.impl.ItemBizImpl;
import com.yunhe.biz.impl.OptionBizImpl;
import com.yunhe.biz.impl.SubjectBizImpl;
import com.yunhe.entity.Item;
import com.yunhe.entity.Option;
import com.yunhe.entity.Subject;
import com.yunhe.entity.User;

/**
 * 投票action
 * */
public class VoteAction  extends ActionSupport implements 
ServletRequestAware,ServletResponseAware,SessionAware,ServletContextAware{
	
	private ServletContext application;
	private HttpServletRequest request;
	private HttpServletResponse response;
	private Map session;
	
	public void setServletRequest(HttpServletRequest request){
		this.request = request;
	}
	public void setServletResponse(HttpServletResponse response){
		this.response = response;
	}
	public void setSession(Map session){
		this.session = session;
	}
	public void setServletContext(ServletContext application){
		this.application = application;
	}
	//创建subject类
	private Subject subject;
	//创建投票选中
	private String[] options;
	
	public Subject getSubject() {
		return subject;
	}


	public void setSubject(Subject subject) {
		this.subject = subject;
	}


	
	public String[] getOptions() {
		return options;
	}
	public void setOptions(String[] options) {
		this.options = options;
	}
	
	private ISubjectBiz sb;
	private IItemBiz ib;
	private IOptionBiz ob;
	
	public ISubjectBiz getSb() {
		return sb;
	}
	public void setSb(ISubjectBiz sb) {
		this.sb = sb;
	}
	public IItemBiz getIb() {
		return ib;
	}
	public void setIb(IItemBiz ib) {
		this.ib = ib;
	}
	public IOptionBiz getOb() {
		return ob;
	}
	public void setOb(IOptionBiz ob) {
		this.ob = ob;
	}
	//投票展示选项
	public String show(){
		//创建subject biz对象
	
		subject = sb.searchSubject(subject.getVs_id());
		return "show";
	}
	//投票结果保存
	public String save(){
	
		//创建记录biz
		
		User user = (User)session.get("user");
		for(int i=0;i<options.length;i++){
			Item item = new Item();
			item.setUser(user);
			item.setSubject(subject);
			Integer io = Integer.parseInt(options[i]);
			Option option = ob.searchOption(io);
			item.setOption(option);
			
			ib.addItem(item);
		}
		
		return "save";
	}
	/**
	 * 展示当前数据
	 * */
	public String view(){
		
		//查询subject
		subject = sb.searchSubject(subject.getVs_id());
		List<Option> list = subject.getOptions();
		Integer s = subject.getItems().size();
		//计算投票数
		Integer size = 0;
		for (Option option : list) {
			Item item = new Item();
			item.setOption(option);
			List<Item> listItem= ib.searchItems(item);
			if(listItem!=null){
				size = listItem.size();
			}else{
				size = 0;
			}
			
			option.setSize(size);
			if(s==0){
				Integer sizePer = 0;
				//赋值
				option.setSizePer(sizePer);
			}
			else{
				Integer sizePer = (size*100/s);
				//赋值
				option.setSizePer(sizePer);
			}
		}
		return "view";
	}

}
